package com.yu1996.base.controller;

import com.yu1996.base.pojo.Label;
import com.yu1996.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin   //跨域注解(用于controller上面)
@RequestMapping("/label")
public class LabelController {


    @Autowired
    private LabelService labelService;

    /**
     * @param
     * @return entity.Result
     * @description 查询所有标签
     * @date 2019/9/1 21:31
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", labelService.findAll());
    }

    /**
     * @param labelId
     * @return entity.Result
     * @description 根据id查询标签
     * @date 2019/9/1 21:31
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findLabelById(@PathVariable String labelId) {
        return new Result(true, StatusCode.OK, "查询成功", labelService.findLabelById(labelId));
    }

    /**
     * @param label
     * @return entity.Result
     * @description 添加标签
     * @date 2019/9/1 21:33
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label) {
        labelService.add(label);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * @param labelId
     * @param label
     * @return entity.Result
     * @description 根据id修改标签
     * @date 2019/9/1 21:36
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public Result update(@PathVariable String labelId, @RequestBody Label label) {
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * @param labelId 标签id
     * @return entity.Result
     * @description 根据id删除标签
     * @date 2019/10/7 19:55
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String labelId) {
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * @param label
     * @return entity.Result
     * @description 条件查询（根据label查询）
     * @date 2019/10/7 19:55
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Label label) {
        List<Label> list = labelService.findSearch(label);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Label label,
                             @PathVariable int page,
                             @PathVariable int size) {
        Page<Label> pageList = labelService.pageQuery(label, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Label>(pageList.getTotalElements(), pageList.getContent()));
    }
}
